// pages/home/details/index.js
var config = require('../../../config');
var strings = require('../../../strings.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //导航栏列表
    navList: [{ id: 1, name: "商品详情" }, { id: 2, name: "用户评价" }, { id: 3, name: "规格参数" }],
    curNav: 0,  //当前选定的导航栏下标
    id: null,   //商品id
    curNav: 0,  //当前选定的导航栏下标
    bannerImgUrls: ['http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg'],  //商品图片展示banner列表
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    reviewedList: [], //评论列表
    infoImg: "",  //商品详情图片
    modelsImg: "",  //规格参数图片
    goodsName: "",  //商品名称
    spec: "", //规格说明
    dateInfo: "", //保质期（日期）
    retailPrice: "",  //实际价格
    price: "",  //上架价格
    cartCount: 0, //购物车商品数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var id = options.id
    this.setData({
      id: id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoodsInfo()
  },
  /**
   * 获取商品详情接口
   */
  getGoodsInfo: function() {
    var that = this
    wx.request({
      url: config.goodsInfo,
      method: 'GET',
      data: {
        id: that.data.id
      },
      success: function(res) {
        that.setData({
          goodsName: res.data.goodsDetail.name,
          spec: res.data.goodsDetail.spec,
          dateInfo: res.data.goodsDetail.dateInfo,
          retailPrice: res.data.goodsDetail.retailPrice,
          price: res.data.goodsDetail.price
        })
      }
    })
  },
  /**
   * 导航栏选择点击事件
   */
  navTap: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    this.setData({
      curNav: index
    })
  },
  /**
   * 点击首页图标，跳转到首页
   */
  goHomeTap: function() {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  /**
   * 点击购物车图标，跳转到购物车
   */
  goShopCarTap: function () {
    wx.navigateTo({
      url: '../shopcar/index/index',
    })
  },
  /**
   * 优惠商城商品。点击加入购物车，进入优惠商城购物车页面
   */
  addBuyerTap: function() {
    wx.request({
      url: config.addCouponsGoods,
      method: 'GET',
      data: {
        goodsId: this.data.id,
        quantity: 1
      },
      success: function(res) {
        console.log(res)
        if(!res.data.msg) {
          wx.showToast({
            title: strings.isAddfail,
            icon: "none"
          })
          return
        }
        wx.navigateTo({
          url: '../shopcar/index/index',
        })
      }
    })
  }
})